/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.all;

import dao.DanhmucDAO;
import dao.SanphamDAO;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.Danhmuc;
import pojo.Sanpham;

/**
 *
 * @author BlueZky
 */
public class timKiemController extends AbstractController {

        //get log4j handler
    private static final Logger logger = Logger.getLogger(timKiemController.class);
    
    public timKiemController() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) {

        ModelAndView mv = null;
        List<Sanpham> danhSach = null;
        String action = request.getParameter("a");

        mv = new ModelAndView("All/index");
        mv.addObject("tag", "timkiem");
        try {

            if (action == null || action.length() == 0) {
            } else if (action.compareTo("s") == 0) {

                String input = request.getParameter("search");
                //log it via log4j
		if(logger.isDebugEnabled()){
			logger.debug(input);
		}
                
                logger.info("Hello word");
                String page = request.getParameter("page");
                String num = request.getParameter("num");
                int soSp = 0;

                if (input != null && input.length() > 0) {
                    soSp = SanphamDAO.laySoSanPhamDonGian(input);
                    if (page == null || page.length() == 0 || !page.matches("^[0-9]*$")
                            || num == null || num.length() == 0 || !num.matches("^[0-9]*$")) {

                        danhSach = SanphamDAO.timDonGian(input, 0, 8);
                        mv.addObject("page", 0);
                        mv.addObject("num", 8);
                        mv.addObject("last", (soSp - 1) / 8);
                    } else {
                        danhSach = SanphamDAO.timDonGian(input, Integer.parseInt(page), Integer.parseInt(num));
                        mv.addObject("page", Integer.parseInt(page));
                        mv.addObject("num", Integer.parseInt(num));
                        mv.addObject("last", (soSp - 1) / Integer.parseInt(num));
                    }

                    mv.addObject("search", input);
                    mv.addObject("danhSach", danhSach);
                    mv.addObject("tag", "ketqua");
                    mv.addObject("a", "s");
                }
                return mv;
            } else if (action.compareTo("a") == 0) {
                String input = request.getParameter("search");
                String catalog = request.getParameter("catalog");
                String subCatalog = request.getParameter("sub-catalog");
                String price = request.getParameter("price");
                String page = request.getParameter("page");
                String num = request.getParameter("num");
                int soSp = 0;

                int iCatalog = -1;
                int iSubCatalog = -1;
                int iPrice = -1;

                iCatalog = checkValidation(catalog);
                iSubCatalog = checkValidation(subCatalog);
                iPrice = checkValidation(price);


                soSp = SanphamDAO.laySoSanPhamNangCao(input, iCatalog, iSubCatalog, iPrice);
                if (page == null || page.length() == 0 || !page.matches("^[0-9]*$")
                        || num == null || num.length() == 0 || !num.matches("^[0-9]*$")) {

                    danhSach = SanphamDAO.timNangCao(input, iCatalog, iSubCatalog, iPrice, 0, 8);
                    mv.addObject("page", 0);
                    mv.addObject("num", 8);
                    mv.addObject("last", (soSp - 1) / 8);
                } else {
                    danhSach = SanphamDAO.timNangCao(input, iCatalog, iSubCatalog, iPrice, Integer.parseInt(page), Integer.parseInt(num));
                    mv.addObject("page", Integer.parseInt(page));
                    mv.addObject("num", Integer.parseInt(num));
                    mv.addObject("last", (soSp - 1) / Integer.parseInt(num));
                }

                mv.addObject("search", input);
                mv.addObject("danhSach", danhSach);
                mv.addObject("tag", "ketqua");
                mv.addObject("a", "a");
                mv.addObject("catalog", catalog);
                mv.addObject("sub-catalog", subCatalog);
                mv.addObject("price", price);

                return mv;
            } else {
            }

            List<Danhmuc> danhMuc = DanhmucDAO.layDanhSach();
            mv.addObject("danhMuc", danhMuc);

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            mv = new ModelAndView("All/index");
        }
        return mv;
    }

    private int checkValidation(String input) {

        if (input == null || input.length() == 0 || !input.matches("^[0-9]*$")) {
            return -1;
        } else {
            return Integer.parseInt(input);
        }
    }
}
